import config from 'config'
import httpFetch from 'share/httpFetch'
import errorMessage from "share/errorMessage";

export default {

  //获取问题分类列表（已发布的问题）
  getAllQuesList (categoryType, language) {
  return httpFetch.get(`${config.baseUrl}/api/commonproblem/list?categoryType=${categoryType}&language=${language}`)
    .catch(err => {
      errorMessage(err.response);
      throw err;
    })
  },

  //获取问题分类列表
  getCategoryList (categoryType, language) {
    return httpFetch.get(`${config.baseUrl}/api/commonproblem/category/list?categoryType=${categoryType}&language=${language}`)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      })
  },

  //获取问题分类详情
  getCategoryDetail (categoryId, language) {
    return httpFetch.get(`${config.baseUrl}/api/commonproblem/category?categoryId=${categoryId}&language=${language}`)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      })
  },

  //问题分类排序
  sortCategoryList (ids) {
    return httpFetch.put(`${config.baseUrl}/api/commonproblem/category/sort`, {ids: ids})
      .catch(err => {
        errorMessage(err.response);
        throw err;
      })
  },


  //问题排序  api/commonproblem/sort
  sortQuesList (dtos) {
    return httpFetch.put(`${config.baseUrl}/api/commonproblem/sort`, dtos)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      })
  },

  //新增问题分类
  onClickAddCategory (item) {
    return httpFetch.post(`${config.baseUrl}/api/commonproblem/category`, item)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      })
  },

  //编辑问题分类
  editCategory (item) {
    return httpFetch.put(`${config.baseUrl}/api/commonproblem/category`, item)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      })
  },

  //删除问题分类
  deleteCategory (categoryId) {
    return httpFetch.delete(`${config.baseUrl}/api/commonproblem/category?categoryId=${categoryId}`)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      })

  },

  //启用禁用问题
  enableCategoryQues (item) {
    return httpFetch.put(`${config.baseUrl}/api/commonproblem/status/batch`, item)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      })
  },

  //启用禁用问题分类
  enableCategory (item) {
    return httpFetch.put(`${config.baseUrl}/api/commonproblem/category/status/batch`, item)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      })
  },

  //根据id获取问题详情
  getQuesDetail (commonProblemId, language) {
    return httpFetch.unsafe.get(`${config.baseUrl}/api/commonproblem/id?commonProblemId=${commonProblemId}&language=${language}`)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      })
  },

  //根据id获取历史版本列表
  getHistoryLists(commonProblemId, language) {
    let item = {
      id: commonProblemId,
      language,
      page: 0,
      size: 10
    };
    return httpFetch.get(`${config.baseUrl}/api/commonproblem/his`, item)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      })
  },

  //根据id获取历史版本详情
  getHistoryDetail(commonProblemId, language) {
    let item = {
      id: commonProblemId,
      language,
    };
    return httpFetch.get(`${config.baseUrl}/api/commonproblem/his/id`, item)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      })
  },

  //根据问题分类类型获取问题列表
  getQuesList (categoryType, language) {
    return httpFetch.get(`${config.baseUrl}/api/commonproblem?categoryType=${categoryType}&language=${language}`)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      })
  },

  //问题发布
  releaseAndPreview (categoryType, publishType) {
    return httpFetch.put(`${config.baseUrl}/api/commonproblem/publish?categoryType=${categoryType}&publishType=${publishType}`)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      })
  },

  //新增问题
  addQues (item) {
    return httpFetch.post(`${config.baseUrl}/api/commonproblem`, item)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      })
  },

  //编辑问题
  editQues (item) {
    return httpFetch.put(`${config.baseUrl}/api/commonproblem`, item)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      })
  },

  //搜索问题
  searchQues (item) {
    return httpFetch.get(`${config.baseUrl}/api/commonproblem/search?categoryType=COMMONPROBLEM`, item)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      })
  },

  //获取权限信息列表
  getAuthorityList () {
    return httpFetch.get(`${config.baseUrl}/api/authority/list`)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      })
  },

  //上传图片
  handleImageUpload(formData) {
    return httpFetch.post(`${config.baseUrl}/api/upload/static/attachment`, formData)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      })
  },

}
